package com.lagou.app;

import com.lagou.entity.Account;
import com.lagou.entity.Transaction;
import com.lagou.utils.DateUtils;
import com.lagou.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class TestTransfer {

    public static void main(String[] args) {

        // 按照步骤 实现卡号：1122334455向55443332211转账5000元的操作
        // 1.手动获取QueryRunner对象，手动提交事务
        Connection con = null;
        try {
            QueryRunner qr = new QueryRunner();
            con = DruidUtils.getConnection();
            con.setAutoCommit(false); // 手动提交事务
            String card1 = "1122334455"; // 转出卡号
            String card2 = "55443332211"; // 转入卡号
            // 2.编写sql 查询1122334455账户余额是否大于5000
            String sql = "select balance from account where card = ?";
            // 3.执行sql
            Account account = qr.query(con, sql, new BeanHandler<>(Account.class), card1);
            if (account.getBalance() > 5000) {
                // 大于5000，执行转账操作
                sql = "update account set balance = balance - 5000 where card = ?";
                int update = qr.update(con, sql, card1);
                sql = "update account set balance = balance + 5000 where card = ?";
                int update1 = qr.update(con, sql, card2);
                if (update > 0 && update1 > 0) { // 代表转账成功
                    // 转账结束后, 将转入、转出记录分别写入到Transaction表中
                    System.out.println("转账成功, 正在生成交易信息...");
                    Thread.sleep(4000); // 延时4s
                    sql = "insert into Transaction values(null,?,?,?,?)";
                    Object[] obj1 = {card1, "转出", 5000.0, DateUtils.getDateFormart()};
                    Transaction t1 = qr.insert(con, sql, new BeanHandler<>(Transaction.class), obj1);
                    sql = "insert into Transaction values(null,?,?,?,?)";
                    Object[] obj2 = {card2, "转入", 5000.0, DateUtils.getDateFormart()};
                    Transaction t2 = qr.insert(con, sql, new BeanHandler<>(Transaction.class), obj2);
                    // t1  t2都不为空，说明转账记录已导入
                    if (null != t1 && null != t2) {
                        sql = "select * from Transaction";
                        List<Transaction> transactionList = qr.query(con, sql, new BeanListHandler<>(Transaction.class));
                        System.out.println("交易已完成, 交易信息如下: ");
                        // 遍历结果集，查看转账流水
                        for (Transaction transaction : transactionList) {
                            System.out.println(transaction);
                        }
                    }
                }
            } else {
                System.out.println("转账失败, 请核对账户余额");
            }
            con.commit(); // 提交事务
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                con.rollback(); // 出现异常进行回滚
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            // 释放资源
            try {
                if (null != con) {
                    con.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
